/****************************************************************************
 * Copyright (C) 2012 Dj_Skual
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/
#include "ListSaveInfos.h"
#include "../../FileOperations/fileops.h"
#include "../../FileOperations/Nand.hpp"
#include "../../Themes/CTheme.h"
#include "../../Saves/SaveTools.h"
#include "../../Gui/gui_banner.h"

SaveInfosWindow::SaveInfosWindow(Browser * b, int w, int h)
	: GuiFrame(w, h)
{
	browser = b;
	browser->IndexChanged.connect(this, &SaveInfosWindow::OnBrowserIndexChange);
	
	fileType = TYPE_UNKNOW;
	selIndex = -1;
	tid = 0;
	
	type_Wii = Resources::GetImageData("icon_type_wii.png");
	type_Wiiware = Resources::GetImageData("icon_type_wiiware.png");
	type_Gamecube = Resources::GetImageData("icon_type_gamecube.png");
	type_VC_NES = Resources::GetImageData("icon_type_vc_nes.png");
	type_VC_SNES = Resources::GetImageData("icon_type_vc_snes.png");
	type_VC_N64 = Resources::GetImageData("icon_type_vc_n64.png");
	type_VC_SMS = Resources::GetImageData("icon_type_vc_sms.png");
	type_VC_MD = Resources::GetImageData("icon_type_vc_md.png");
	type_VC_PCE = Resources::GetImageData("icon_type_vc_pce.png");
	type_VC_Neogeo = Resources::GetImageData("icon_type_vc_neogeo.png");
	type_VC_Arcade = Resources::GetImageData("icon_type_vc_arcade.png");
	type_VC_C64 = Resources::GetImageData("icon_type_vc_c64.png");
	type_Channel = Resources::GetImageData("icon_type_channel.png");
	type_Unknow = Resources::GetImageData("icon_type_unknow.png");
	
	iconImg = NULL;
	typeImg = NULL;
	
	nameTxt = new GuiText((char*)NULL, thInt("16 - mainbrowser browser infos text size"), thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	nameTxt->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	nameTxt->SetMaxWidth(width, SCROLL_HORIZONTAL);
	
	subnameTxt = new GuiText((char*)NULL, thInt("16 - mainbrowser browser infos text size"), thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	subnameTxt->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	subnameTxt->SetMaxWidth(width, DOTTED);
	
	idTxt = new GuiText((char*)NULL, thInt("16 - mainbrowser browser infos text size"), thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	idTxt->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	idTxt->SetMaxWidth(width, DOTTED);
	
	sizeTxt = new GuiText((char*)NULL, thInt("16 - mainbrowser browser infos text size"), thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	sizeTxt->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	sizeTxt->SetMaxWidth(width, DOTTED);
	
	path = new GuiText(tr( "Path:" ), thInt("16 - mainbrowser browser infos text size"), thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	path->SetAlignment(ALIGN_LEFT | ALIGN_BOTTOM);
	path->SetPosition(0, -57);
	
	pathTxt = new GuiText((char*)NULL, thInt("16 - mainbrowser browser infos text size"), thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	pathTxt->SetLinesToDraw(3);
	pathTxt->SetInterline(3);
	pathTxt->SetMaxWidth(width, WRAP);
	pathTxt->SetAlignment(ALIGN_LEFT | ALIGN_BOTTOM);
	pathTxt->SetPosition(0, -38);
}

SaveInfosWindow::~SaveInfosWindow()
{
	RemoveAll();
	
	if(iconImg)
		delete iconImg;
	if(typeImg)
		delete typeImg;
	delete nameTxt;
	delete subnameTxt;
	delete idTxt;
	delete sizeTxt;
	delete path;
	delete pathTxt;
	
	Resources::Remove(type_Wii);
	Resources::Remove(type_Wiiware);
	Resources::Remove(type_Gamecube);
	Resources::Remove(type_VC_NES);
	Resources::Remove(type_VC_SNES);
	Resources::Remove(type_VC_N64);
	Resources::Remove(type_VC_SMS);
	Resources::Remove(type_VC_MD);
	Resources::Remove(type_VC_PCE);
	Resources::Remove(type_VC_Neogeo);
	Resources::Remove(type_VC_Arcade);
	Resources::Remove(type_VC_C64);
	Resources::Remove(type_Channel);
	Resources::Remove(type_Unknow);
}

void SaveInfosWindow::OnBrowserIndexChange()
{
	SetInfos();
}

void SaveInfosWindow::SetInfos()
{
	RemoveAll();
	
	if(iconImg)
		delete iconImg;
	iconImg = NULL;
	
	if(typeImg)
		delete typeImg;
	typeImg = NULL;
	
	if(!browser->GetEntrieCount())
		return;
	
	selIndex = browser->GetSelIndex();
	
	tid = browser->GetSaveTid(selIndex);
	
	GetSaveIcon();
	
	GetTypeIcon();
	
	int y = iconImg->GetHeight()+7;
	nameTxt->SetText(browser->GetSaveName(selIndex));
	nameTxt->SetPosition(0, y);
	y += 19;
	
	subnameTxt->SetText(browser->GetSaveSubname(selIndex));
	subnameTxt->SetPosition(0, y);
	y += 25;
	
	std::string ID = tr("ID:");
	ID += " ";
	ID += browser->GetSaveID(selIndex);
	idTxt->SetText(ID);
	idTxt->SetPosition(0, y);
	y += 19;
	
	std::string size = tr("Size:");
	size += fmt(" %0.f %s", ceil(browser->GetSaveBlocks(selIndex)), tr( "Block(s)" ));
	sizeTxt->SetText(size);
	sizeTxt->SetPosition(0, y);
	
	std::string savePath = "";
	if(Settings.LoadedBrowser == SAVE_BROWSER)
		savePath = fmt("isfs:/title/%08x/%08x/data", TITLE_UPPER(tid), TITLE_LOWER(tid));
	else
		savePath = fmt("%s/title/%08x/%08x/data", browser->GetCurrentPath().c_str(), TITLE_UPPER(tid), TITLE_LOWER(tid));
	pathTxt->SetText(savePath);
	
	Append(nameTxt);
	Append(subnameTxt);
	Append(idTxt);
	Append(iconImg);
	Append(sizeTxt);
	Append(path);
	Append(pathTxt);
}

void SaveInfosWindow::GetSaveIcon()
{
	ByteArray buffer;
	
	if(Settings.LoadedBrowser == SAVE_BROWSER)
	{
		std::string savepath = fmt("/title/%08x/%08x/data/banner.bin", TITLE_UPPER(tid), TITLE_LOWER(tid));
		Nand::LoadFile(savepath, buffer);
	}
	else
	{
		std::string savepath = fmt("%s/title/%08x/%08x/data/banner.bin", browser->GetCurrentPath().c_str(), TITLE_UPPER(tid), TITLE_LOWER(tid));
		LoadFileToMem(savepath, buffer);
	}
	
	iconImg = new GuiSaveIcon(buffer);
}

void SaveInfosWindow::FoundSaveType()
{
	fileType = TYPE_UNKNOW;
	
	std::string ID = browser->GetSaveID(selIndex);
	if(ID.empty())
		return;
	
	std::string id(ID);
	std::string id2(ID);
	id.erase(1);
	id2.erase(2);
	
	if(!id.compare("R") || !id.compare("S") || !id.compare("D"))
		fileType = TYPE_WII;
	else if(!id.compare("W"))
		fileType = TYPE_WIIWARE;
	else if(!id.compare("G") || !id.compare("P") || !id.compare("D") || !id.compare("U"))
		fileType = TYPE_GAMECUBE;
	else if(!id.compare("F"))
		fileType = TYPE_VC_NES;
	else if(!id.compare("J"))
		fileType = TYPE_VC_SNES;
	else if(!id.compare("N"))
		fileType = TYPE_VC_N64;
	else if(!id.compare("L"))
		fileType = TYPE_VC_SMS;
	else if(!id.compare("M"))
		fileType = TYPE_VC_MD;
	else if(!id.compare("P"))
		fileType = TYPE_VC_PCE;
	else if(!id.compare("E"))
	{
		if(!id2.compare("EA") || !id2.compare("EB"))
			fileType = TYPE_VC_NEOGEO;
		else
			fileType = TYPE_VC_ARCADE;
	}
	else if(!id.compare("C"))
		fileType = TYPE_VC_C64;
	else if(!id.compare("H") || !id2.compare("JO"))
		fileType = TYPE_CHANNEL;
}

void SaveInfosWindow::GetTypeIcon()
{
	FoundSaveType();
	
	switch (fileType)
	{
		case TYPE_WII:
			typeImg = new GuiImage(type_Wii);
			break;
		case TYPE_WIIWARE:
			typeImg = new GuiImage(type_Wiiware);
			break;
		case TYPE_GAMECUBE:
			typeImg = new GuiImage(type_Gamecube);
			break;
		case TYPE_VC_NES:
			typeImg = new GuiImage(type_VC_NES);
			break;
		case TYPE_VC_SNES:
			typeImg = new GuiImage(type_VC_SNES);
			break;
		case TYPE_VC_N64:
			typeImg = new GuiImage(type_VC_N64);
			break;
		case TYPE_VC_SMS:
			typeImg = new GuiImage(type_VC_SMS);
			break;
		case TYPE_VC_MD:
			typeImg = new GuiImage(type_VC_MD);
			break;
		case TYPE_VC_PCE:
			typeImg = new GuiImage(type_VC_PCE);
			break;
		case TYPE_VC_NEOGEO:
			typeImg = new GuiImage(type_VC_Neogeo);
			break;
		case TYPE_VC_ARCADE:
			typeImg = new GuiImage(type_VC_Arcade);
			break;
		case TYPE_VC_C64:
			typeImg = new GuiImage(type_VC_C64);
			break;
		case TYPE_CHANNEL:
			typeImg = new GuiImage(type_Channel);
			break;
		case TYPE_UNKNOW:
		default:
			typeImg = new GuiImage(type_Unknow);
			break;
	}
	
	typeImg->SetAlignment(ALIGN_RIGHT | ALIGN_TOP);
	Append(typeImg);
}
